package com.example.demo.netty.myprotocol;

import com.example.demo.netty.simpledemo.MyClientHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

/**
 * <内容说明>
 *
 * @author yu.guangqi
 * created on 2019/11/8 16:12
 */
public class MyClient {
    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();

        Bootstrap bootstrap = new Bootstrap()
                .group(eventLoopGroup)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new MyByteToLongDecoder());
                        pipeline.addLast(new MyLongToByteEncoder());
                        pipeline.addLast(new MyProtocolClientHandler());
                    }
                });
        Channel channel = bootstrap.connect("localhost", 8888).sync().channel();

//        channel.writeAndFlush("hello from client");
    }
}
